package com.example.jegotrip.adapter;

import static android.view.View.GONE;

import android.annotation.SuppressLint;
import android.graphics.Typeface;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.example.jegotrip.R;
import com.example.jegotrip.common.CornerImageView;
import com.example.jegotrip.entity.CartInfo;

import java.util.ArrayList;
import java.util.List;

public class CartListAdapter extends RecyclerView.Adapter<CartListAdapter.CartListViewHolder> {

    List<CartInfo> cartInfos = new ArrayList<>();

    @SuppressLint("NotifyDataSetChanged")
    public CartListAdapter(List<CartInfo> cartInfos) {
        this.cartInfos = cartInfos;
        notifyDataSetChanged();
    }

    @SuppressLint("NotifyDataSetChanged")

    public void setCartInfos(List<CartInfo> cartInfos) {
        this.cartInfos = cartInfos;
        notifyDataSetChanged();
    }

    @NonNull
    @Override
    public CartListViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
//      onCreateViewHolder  ViewGroup parent   参数要传null就不会导致仅仅进一次onCreateViewHolder方法，就可以展示全数据
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.cart_list_view,null,false);
        //点击noteInfo 查看详情页面进行传值
        CartListViewHolder holder = new CartListViewHolder(view);

        return holder;
    }

    @Override
    @SuppressLint({"RecyclerView", "SetTextI18n"})
    public void onBindViewHolder(@NonNull CartListViewHolder holder, int position) {
        CartInfo cartInfo = cartInfos.get(position);
//        设置数据
        holder.cart_product_img.setImageURL(cartInfo.getProduct_img());
        holder.cart_product_title.setText(cartInfo.getProduct_title());
        holder.cart_product_price.setText(cartInfo.getProduct_price() + "");
        holder.cart_checkbox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean isChecked) {
                if (mCartListOnItemClickListener != null){
                    mCartListOnItemClickListener.onCartAddOrderMoreItemClick(position,cartInfo);
                }
            }
        });
        holder.cart_more.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (mCartListOnItemClickListener != null){
                    mCartListOnItemClickListener.onCartMoreItemClick(position,cartInfo);
                }
            }
        });

        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                if (mCartListOnItemClickListener != null){
                    mCartListOnItemClickListener.onCartItemClick(position,cartInfo);
                }
            }
        });
    }

    @Override
    public int getItemCount() {
        return cartInfos.size();
    }

    static class CartListViewHolder extends RecyclerView.ViewHolder{
        // 添加ViewHolder中的控件
        CornerImageView cart_product_img;
        TextView cart_product_title;
        TextView cart_product_price;
        ImageView cart_more;
        CheckBox cart_checkbox;
        public CartListViewHolder(@NonNull View itemView) {
            super(itemView);
            cart_product_img = itemView.findViewById(R.id.cart_product_img);
            cart_product_title = itemView.findViewById(R.id.cart_product_title);
            cart_product_price = itemView.findViewById(R.id.cart_product_price);
            cart_more = itemView.findViewById(R.id.cart_more);
            cart_checkbox = itemView.findViewById(R.id.cart_checkbox);
        }
    }

    public void setmCartListOnItemClickListener(CartListOnItemClickListener mCartListOnItemClickListener) {
        this.mCartListOnItemClickListener = mCartListOnItemClickListener;
    }

    private CartListOnItemClickListener mCartListOnItemClickListener;

    public interface CartListOnItemClickListener {

        void onCartItemClick(int position, CartInfo cartInfo);
        void onCartMoreItemClick(int position, CartInfo cartInfo);
        void onCartAddOrderMoreItemClick(int position, CartInfo cartInfo);
    }
}
